@@ -1,6 +1,7 @@ |
||
| 1 | 1 |
module Agents |
| 2 | 2 |
class SlackAgent < Agent |
| 3 | 3 |
DEFAULT_USERNAME = 'Huginn' |
| 4 |
+ ALLOWED_PARAMS = ['channel', 'username', 'unfurl_links', 'attachments'] |
|
| 4 | 5 |
|
| 5 | 6 |
cannot_be_scheduled! |
| 6 | 7 |
cannot_create_events! |
@@ -13,7 +14,7 @@ module Agents |
||
| 13 | 14 |
#{'## Include `slack-notifier` in your Gemfile to use this Agent!' if dependencies_missing?}
|
| 14 | 15 |
|
| 15 | 16 |
To get started, you will first need to configure an incoming webhook. |
| 16 |
- |
|
| 17 |
+ |
|
| 17 | 18 |
- Go to `https://my.slack.com/services/new/incoming-webhook`, choose a default channel and add the integration. |
| 18 | 19 |
|
| 19 | 20 |
Your webhook URL will look like: `https://hooks.slack.com/services/some/random/characters` |
@@ -65,14 +66,22 @@ module Agents |
||
| 65 | 66 |
@slack_notifier ||= Slack::Notifier.new(webhook_url, username: username) |
| 66 | 67 |
end |
| 67 | 68 |
|
| 69 |
+ def filter_options(opts) |
|
| 70 |
+ opts.select { |key, value| ALLOWED_PARAMS.include? key }.symbolize_keys
|
|
| 71 |
+ end |
|
| 72 |
+ |
|
| 68 | 73 |
def receive(incoming_events) |
| 69 | 74 |
incoming_events.each do |event| |
| 70 | 75 |
opts = interpolated(event) |
| 71 |
- if /^:/.match(opts[:icon]) |
|
| 72 |
- slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_emoji: opts[:icon], unfurl_links: opts[:unfurl_links] |
|
| 73 |
- else |
|
| 74 |
- slack_notifier.ping opts[:message], channel: opts[:channel], username: opts[:username], icon_url: opts[:icon], unfurl_links: opts[:unfurl_links] |
|
| 76 |
+ slack_opts = filter_options(opts) |
|
| 77 |
+ if opts[:icon].present? |
|
| 78 |
+ if /^:/.match(opts[:icon]) |
|
| 79 |
+ slack_opts[:icon_emoji] = opts[:icon] |
|
| 80 |
+ else |
|
| 81 |
+ slack_opts[:icon_url] = opts[:icon] |
|
| 82 |
+ end |
|
| 75 | 83 |
end |
| 84 |
+ slack_notifier.ping opts[:message], slack_opts |
|
| 76 | 85 |
end |
| 77 | 86 |
end |
| 78 | 87 |
end |
@@ -2,11 +2,14 @@ require 'spec_helper' |
||
| 2 | 2 |
|
| 3 | 3 |
describe Agents::SlackAgent do |
| 4 | 4 |
before(:each) do |
| 5 |
+ @fallback = "Its going to rain" |
|
| 6 |
+ @attachments = [{'fallback' => "{{fallback}}"}]
|
|
| 5 | 7 |
@valid_params = {
|
| 6 | 8 |
'webhook_url' => 'https://hooks.slack.com/services/random1/random2/token', |
| 7 | 9 |
'channel' => '#random', |
| 8 | 10 |
'username' => "{{username}}",
|
| 9 |
- 'message' => "{{message}}"
|
|
| 11 |
+ 'message' => "{{message}}",
|
|
| 12 |
+ 'attachments' => @attachments |
|
| 10 | 13 |
} |
| 11 | 14 |
|
| 12 | 15 |
@checker = Agents::SlackAgent.new(:name => "slacker", :options => @valid_params) |
@@ -15,7 +18,7 @@ describe Agents::SlackAgent do |
||
| 15 | 18 |
|
| 16 | 19 |
@event = Event.new |
| 17 | 20 |
@event.agent = agents(:bob_weather_agent) |
| 18 |
- @event.payload = { :channel => '#random', :message => 'Looks like its going to rain', username: "Huggin user"}
|
|
| 21 |
+ @event.payload = { :channel => '#random', :message => 'Looks like its going to rain', username: "Huggin user", fallback: @fallback}
|
|
| 19 | 22 |
@event.save! |
| 20 | 23 |
end |
| 21 | 24 |
|
@@ -44,12 +47,22 @@ describe Agents::SlackAgent do |
||
| 44 | 47 |
@checker.options['icon_emoji'] = "something" |
| 45 | 48 |
expect(@checker).to be_valid |
| 46 | 49 |
end |
| 50 |
+ |
|
| 51 |
+ it "should allow attachments" do |
|
| 52 |
+ @checker.options['attachments'] = nil |
|
| 53 |
+ expect(@checker).to be_valid |
|
| 54 |
+ @checker.options['attachments'] = [] |
|
| 55 |
+ expect(@checker).to be_valid |
|
| 56 |
+ @checker.options['attachments'] = @attachments |
|
| 57 |
+ expect(@checker).to be_valid |
|
| 58 |
+ end |
|
| 47 | 59 |
end |
| 48 | 60 |
|
| 49 | 61 |
describe "#receive" do |
| 50 | 62 |
it "receive an event without errors" do |
| 51 | 63 |
any_instance_of(Slack::Notifier) do |obj| |
| 52 | 64 |
mock(obj).ping(@event.payload[:message], |
| 65 |
+ attachments: [{'fallback' => @fallback}],
|
|
| 53 | 66 |
channel: @event.payload[:channel], |
| 54 | 67 |
username: @event.payload[:username] |
| 55 | 68 |
) |